import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/user/user_income_info_model.dart';
import 'package:tiktokadult/hj_page/mine_business/make_money/withdrawal_page.dart';
import 'package:tiktokadult/hj_page/mine_business/welfare/share_details_page.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

class IncomeData {
  String value;
  String title;

  IncomeData(this.title, this.value);
}

class ShareDataListPage extends StatefulWidget {
  const ShareDataListPage({super.key});

  @override
  State<ShareDataListPage> createState() => _ShareDataListPageState();
}

class _ShareDataListPageState extends State<ShareDataListPage> {
  UserIncomeModel? _model;
  bool isLoading = true;
  final incomeList = <IncomeData>[];
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final res = await MineService.fetchAllIncomeInfo();
      isLoading = false;
      _model = res;
      incomeList.add(IncomeData("当月收益(元)", _model?.monthIncomeAmount ?? '0'));
      incomeList.add(IncomeData("当月业绩(元)", _model?.monthIncomePerformance ?? '0'));
      incomeList.add(IncomeData("当月推广数", _model?.monthInviteUserCount ?? '0'));
      incomeList.add(IncomeData("今日收益(元)", _model?.todayIncomeAmount ?? '0'));
      incomeList.add(IncomeData("今日业绩(元)", _model?.todayIncomePerformance ?? '0')); 
      incomeList.add(IncomeData("今日推广数", _model?.todayInviteUserCount ?? '0'));
      if (mounted) setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('推广数据'),
          actions: [
            GestureDetector(
              onTap: () => Get.to(ShareDetailsPage(), arguments: _model),
              child: Text(
                '收益明细',
                style: TextStyle(color: Color(0xFF989898), fontSize: 12),
              ),
            ),
            16.sizeBoxW
          ],
        ),
        body: () {
          if (isLoading) return LoadingCenterWidget();
          if (_model == null) return CErrorWidget();
          return Padding(
            padding: EdgeInsets.only(left: 12, right: 12, top: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 125,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/hj_mine/promotional_data_header_bg.webp"),
                      fit: BoxFit.cover,
                    ),
                      borderRadius: BorderRadius.circular(9)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      14.sizeBoxH,
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Expanded(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  "钱包余额",
                                  style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w600, fontSize: 14.0),
                                ),
                                4.sizeBoxH,
                                Text(
                                  "${_model?.totalAmount}",
                                  style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 24.0),
                                )
                              ],
                            ),
                          ), 
                          Expanded(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  "累积收益",
                                  style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w600, fontSize: 14.0),
                                ),
                                4.sizeBoxH,
                                Text(
                                  "${_model?.totalIncomeAmount}",
                                  style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 24.0),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                      11.sizeBoxH,
                      InkWell(
                        onTap: () => Get.to(WithdrawalPage()),
                        child: Container(
                          width: 72,
                          height: 25, 
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(6)), 
                            gradient: LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [Color(0xFFEDBB95), Color(0xFFFADCCC)],
                            ),
                          ),
                          child: Center(
                            child: Text(
                              "立即提现",
                              style: const TextStyle(color: const Color(0xff7A471B), fontWeight: FontWeight.w500, fontSize: 12.0),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                12.sizeBoxH,
                Container(
                  padding: EdgeInsets.symmetric(vertical: 10),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(9)), 
                    color: Color(0xFF1A1B21),
                  ),
                  child: Column(
                    children: [
                      Stack(
                        alignment: Alignment.center,
                        children:[
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Container(
                                width: 1,
                                height: 38,
                                color: Color(0x1AFFFFFF),
                              ), 
                              Container(
                            width: 1,
                            height: 38,
                            color: Color(0x1AFFFFFF),
                          ),
                            ],
                          ),
                          Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: incomeList.take(3).map((e) => _buildIncomeGItem(e)).toList(),
                      ),
                        ]
                      ),
                      Container(
                        height: 0.5,
                        margin: EdgeInsets.symmetric(vertical: 18, horizontal: 10),
                        color: Color(0x1AFFFFFF),
                      ),
                      Stack(alignment: Alignment.center, children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Container(
                            width: 1,
                            height: 38,
                            color: Color(0x1AFFFFFF),
                          ),
                          Container(
                            width: 1,
                            height: 38,
                            color: Color(0x1AFFFFFF),
                          ),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: incomeList.skip(3).map((e) => _buildIncomeGItem(e)).toList(),
                      ),
                    ]),
                    ]), 
                ),
                15.sizeBoxH,
                Text(
                  "直推总统计",
                  style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w600, fontSize: 16.0),
                ),
                15.sizeBoxH,
                _buildTGItem("直推用户数", _model?.totalInviteUserCount ?? '0', true),
                _buildTGItem("直推付费用户", _model?.totalPayUserCount ?? '0', true),
                _buildTGItem("直推下级代理", _model?.totalInviteNextProxy ?? '0', false),
              ],
            ),
          );
        }());
  }

  Widget _buildTGItem(String title, String value, bool hasDivider) {
    return Container(
      height: 32,
      padding: EdgeInsets.only(bottom: 12),
      margin: EdgeInsets.only(top: 15),
      decoration: BoxDecoration(
        border: hasDivider ? Border(bottom: BorderSide(color: Color(0x1AFFFFFF))) : null,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: const TextStyle(color: const Color(0xffDCDCDC), fontWeight: FontWeight.w400, fontSize: 14.0),
          ),
          Text(
            "$value",
            style: const TextStyle(color: const Color(0x8CFFFFFF), fontWeight: FontWeight.w400, fontSize: 14.0),
          ),
        ],
      ),
    );
  }

  Widget _buildIncomeGItem(IncomeData item) {
    return Container(
      height: 60, 
      width: 72, 
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "${item.value}",
            textAlign: TextAlign.center,
            style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w600, fontSize: 24.0),
          ), 
          Text(
            item.title,
            textAlign: TextAlign.center,
            style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w400, fontSize: 12.0),
          )
        ],
      ),
    );
  }
}
